package com.mypage.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.mypage.bean.QueryCustomListBean;

import java.util.List;

/**
 * Created by liuyongjie on 2017/5/11.
 */

public class NChageListAdapter extends BaseAdapter {

    public List<QueryCustomListBean.CustomData.Custom>  mList;
    public Context context;
    public String search;
    public NChageListAdapter(List<QueryCustomListBean.CustomData.Custom>  mQList, Context context, String search){
        this.mList = mQList;
        this.context = context;
        this.search = search;
    }

    @Override
    public int getCount() {
        return mList.size()==0?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.custom_list_item,null);
            viewHolder.customName = (TextView) convertView.findViewById(R.id.customName);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int index;
        ForegroundColorSpan span = new ForegroundColorSpan(context.getResources().getColor(R.color.bg_b));//要显示的颜色
        SpannableStringBuilder builder = new SpannableStringBuilder(mList.get(position).name.toString());

        index = mList.get(position).name.toString().indexOf(search);//从第几个匹配上
        if(index!=-1){//有这个关键字用builder显示
            builder.setSpan(span, index, index+search.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHolder.customName.setText(builder);
        }else{//没有则直接显示
            viewHolder.customName.setText(mList.get(position).name.toString().trim());
        }

        return convertView;
    }

    public class ViewHolder{
        TextView customName;
    }
}
